<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <j-dict-select-tag v-model="model.sex" placeholder="请选择性别" dictCode="sex" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="手机号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入手机号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="被拜访人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="visiteUser">
              <a-input v-model="model.visiteUser_dictText" placeholder="请输入被拜访人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="拜访时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="visiteTime">
              <j-date placeholder="请选择拜访时间" :dateFormat="'yyyy-MM-DD HH:mm:ss'"  v-model="model.visiteTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="拜访原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="visiteReason">
              <a-input v-model="model.visiteReason" placeholder="请输入拜访原因"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idCard">
              <a-input v-model="model.idCard" placeholder="请输入身份证号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="depart">
              <a-input v-model="model.depart" placeholder="请输入单位"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="随访人数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="peopleNum">
              <a-input-number v-model="model.peopleNum" placeholder="请输入随访人数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="车牌号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="carNum">
              <a-input v-model="model.carNum" placeholder="请输入车牌号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="体温" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bodyTemp">
              <a-input-number v-model="model.bodyTemp" placeholder="请输入体温" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalState">
              <a-input-number v-model="filterDictTextByCacheText" placeholder="请输入审批状态" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvar">
              <a-input v-model="model.approvar_dictText"></a-input>
            </a-form-model-item>
          </a-col>
          <div v-if="disabled && model.approvalState != 1">
            <a-col :span="24" >
              <a-form-model-item label="审批时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalTime">
                <a-input v-model="model.approvalTime" ></a-input>
              </a-form-model-item>
            </a-col>
             <a-col :span="24">
              <a-form-model-item label="审批结果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalResult">
                <a-input v-model="transformApprovalResult" placeholder="请输入审批结果" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="审批信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalInfo">
                <a-textarea v-model="model.approvalInfo" rows="4" placeholder="请输入审批信息" />
              </a-form-model-item>
            </a-col>
          </div>
        </a-row>
      </a-form-model>
    </j-form-container>
    <a-form-model :model="model"  v-if="approvlStatus && !disabled ">
      <a-row>
        <a-col :span="24">
          <a-form-model-item label="审批结果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalResult">
            <j-dict-select-tag type="list" v-model="model.approvalResult" dictCode="approval_result" placeholder="请选择审批结果" />
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item label="审批信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalInfo">
            <a-textarea v-model="model.approvalInfo"  rows="4" placeholder="审批信息" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { filterDictTextByCache} from '@/components/dict/JDictSelectUtil'
  export default {
    name: 'SkVisitForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/management/skVisit/add",
          edit: "/management/skVisit/edit",
          queryById: "/management/skVisit/queryById"
        },
        approvalList:[],
        approvlStatus:null
      }
    },
    computed: {
      formDisabled(){
        return this.approvlStatus
      },
      filterDictTextByCacheText(){
        return filterDictTextByCache("approval_state",this.model.approvalState)
      },
      transformApprovalResult(){
        return filterDictTextByCache("approval_result",this.model.approvalResult)
      }
    },
    
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getApproval();
      
    },
    methods: {
      // 获取审批人
      getApproval(){
        const url = "/management/teacher/list"
        getAction(url).then((res)=>{
          if(res.success){
            this.approvalList = res.result;
          } else {
            this.$message.warning(res.message);
          }
        }).catch((error)=>{
          console.error(error);
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record,approvlStatus) {
        this.model = Object.assign({}, record);
        // 审核状态
        this.approvlStatus = approvlStatus;
        if (this.disabled) {
          this.approvlStatus = true;
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>